package topcbl.taxi;

import topcbl.util.GPSTracker;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapFragment extends Fragment {
	GoogleMap map;
	ImageButton btnGetLocation;
	GPSTracker gpsTracker;
	Marker marker;
	Button btnNhanh, btnMienPhi;
	//
	SharedPreferences preferences;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_map, container,
				false);
		gpsTracker = new GPSTracker(getActivity());
		createMap();
		
		// --- find user location firstly
		map.clear();
		preferences = getActivity().getSharedPreferences("my_latlng", 0);
		gpsTracker.setLocation(new LatLng((double) preferences.getFloat(
				"Latitude", 14.058324F), (double) preferences.getFloat(
				"Longitude", 108.277199F)));
		marker = putMarkerOnMap(gpsTracker.getLatitude(),
				gpsTracker.getLongitude(), R.drawable.user_pin);
		marker.showInfoWindow();
		map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
				gpsTracker.getLatitude(), gpsTracker.getLongitude()), 16));
		((MainActivity) getActivity()).setAddress(gpsTracker
				.getAddressLine(getActivity()));
		
		// --- Button
		btnGetLocation = (ImageButton) rootView
				.findViewById(R.id.btnGetLocation);
		btnGetLocation.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (gpsTracker.canGetLocation()) {
					map.clear();
					gpsTracker.updateGPSCoordinates();
					marker = putMarkerOnMap(gpsTracker.getLatitude(),
							gpsTracker.getLongitude(), R.drawable.user_pin);
					marker.showInfoWindow();
					map.animateCamera(CameraUpdateFactory.newLatLngZoom(
							new LatLng(gpsTracker.getLatitude(), gpsTracker
									.getLongitude()), 16));
					((MainActivity) getActivity()).setAddress(gpsTracker
							.getAddressLine(getActivity()));
				} else {
					gpsTracker.showSettingsAlert();
				}
			}
		});
		btnNhanh = (Button) rootView.findViewById(R.id.btn_goinhanh);
		btnMienPhi = (Button) rootView.findViewById(R.id.btn_goimienphi);
		btnNhanh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new QuickCallDialog(getActivity()).show();
			}
		});
		btnMienPhi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new CallTaxiDialog(getActivity(), gpsTracker.getLatitude(),
						gpsTracker.getLongitude()).show();
			}
		});
		return rootView;
	}
	
	private void createMap() {
		map = ((SupportMapFragment) getFragmentManager().findFragmentById(
				R.id.map)).getMap();
		// map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
		// map.setMapType(GoogleMap.MAP_TYPE_NONE);
		map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		// map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		// map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
		// --- set Event to put marker when user long click
		map.setOnMapLongClickListener(new OnMapLongClickListener() {

			@Override
			public void onMapLongClick(LatLng point) {
				map.clear();
				map.addMarker(new MarkerOptions()
						.position(point)
						.title(getString(R.string.questionIsHere))
						.icon(BitmapDescriptorFactory
								.fromResource(R.drawable.user_pin)));
				gpsTracker.setLocation(point);
				String address = gpsTracker.getAddressLine(getActivity());
				if (address == null) {
					address = String.format("%.3f : %.3f", point.latitude,
							point.longitude);
					Toast.makeText(getActivity(),
							getActivity().getString(R.string.actionbar_toast),
							Toast.LENGTH_LONG).show();
				} else {
					((MainActivity) getActivity()).setAddress(address);
				}
			}
		});
	}

	public Marker putMarkerOnMap(double lat, double lon, int logo) {
		if (map != null) {
			map.clear();
			MarkerOptions mp = new MarkerOptions();
			mp.position(new LatLng(lat, lon));
			mp.title(getString(R.string.markertext));
			mp.icon(BitmapDescriptorFactory.fromResource(logo));
			return map.addMarker(mp);
		}
		return null;
	}

	@Override
	public void onDestroyView() {
		SupportMapFragment f = (SupportMapFragment) getFragmentManager()
				.findFragmentById(R.id.map);

		if (f != null) {
			try {
				getFragmentManager().beginTransaction().remove(f).commit();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		super.onDestroyView();
	}

	@Override
	public void onPause() {
		SharedPreferences.Editor editor = preferences.edit();
		editor.putFloat("Latitude", (float) gpsTracker.getLatitude());
		editor.putFloat("Longitude", (float) gpsTracker.getLongitude());
		editor.commit();
		super.onPause();
	}
}
